import 'package:flutter_module/common/base/logic/BasicLogic.dart';
import 'package:flutter_module/common/http/ApiConstant.dart';
import 'package:flutter_module/common/http/YZHttpUtils.dart';
import 'package:flutter_module/model/invite_activity.dart';
import 'package:flutter_module/model/invite_record.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class ApplyHistoryLogic extends BasicLogic {
  /// 邀约记录企业列表 列名
  List<String> orgColumns = ['姓名', '申请时间'];

  /// 邀约记录学校列表 列名
  List<String> schoolColumns = ['姓名', '学号', '专业', '申请时间'];

  /// 活动列表
  List<InviteActivity>? actNames;

  /// 当前选中的活动
  Rx<InviteActivity?> selectedAct = Rx(null);

  /// 邀约列表
  List<InviteList> inviteList = [];

  /// 所选的类型：1-本日 2-本周 3-本月 4-累计
  Rx<int> selectedType = Rx(1);

  /// 申请人数
  Rx<int> applyCount = Rx(0);

  int page = 1;

  int pageSize = 20;

  final RefreshController controller = RefreshController(
    initialRefresh: false,
  );

  /// 当前选中的活动角标
  int get selectedActPosition {
    if (actNames?.isEmpty == true || selectedAct.value == null) {
      return 0;
    }
    for (var i = 0; i < actNames!.length; i++) {
      var item = actNames![i];
      if (item.actId == selectedAct.value?.actId) {
        return i;
      }
    }
    return 0;
  }

  @override
  Future<void> onInit() async {
    super.onInit();

    fetchActivityNames();
  }

  /// 获取活动名称列表
  void fetchActivityNames() {
    HttpUtils.get(api_activity_names, successCallBack: (data, total) {
      actNames = (data as List).map((e) => InviteActivity.fromJson(e)).toList();
      if (actNames?.isNotEmpty == true) {
        selectedAct.value = actNames![0];
      }
      fetchInviteRecord();
    }, errorCallBack: (e) {
      //fixme {"code":200,"message":"操作成功","data":null,"traceId":null,"total":null,"sysTime":1685354352440} 会走error
      selectedAct.value = InviteActivity(actId: 0, actName: '暂无活动');
      fetchInviteRecord();
    });
  }

  /// 获取邀约列表
  /// 查询类型 1-本日 2-本周 3-本月 4-累计
  void fetchInviteRecord({bool isRefresh = true}) {
    if (isRefresh) {
      page = 1;
    } else {
      page++;
    }

    var params = {
      "page": page,
      "pageSize": pageSize,
      "dayType": selectedType.value,
      if (selectedAct.value?.actId != 0) "actId": selectedAct.value?.actId ?? 0
    };
    HttpUtils.get(api_invite_record, params: params,
        successCallBack: (data, total) {
      InviteRecord record = InviteRecord.fromJson(data);
      if (isRefresh) {
        inviteList = record.inviteList ?? List.empty();
        applyCount(record.inviteCount);
      } else {
        if (record.inviteList?.isNotEmpty == true) {
          inviteList.addAll(record.inviteList!);
        }
      }

      if (inviteList.length >= (record.inviteCount ?? 0)) {
        controller.loadNoData();
      } else {
        controller.loadComplete();
      }

      update();
    }, errorCallBack: (e) {});
  }

  /// 是否为学校邀约类型
  bool isSchoolType() {
    for (var item in inviteList) {
      if (item.studentNumber?.isNotEmpty == true) {
        return true;
      }
    }
    return false;
  }

  /// 今日 本周切换
  void changeDayType(int index) {
    controller.resetNoData();
    selectedType(index);
    fetchInviteRecord();
  }

  /// 活动切换
  void changeAct(int index) {
    controller.resetNoData();
    selectedAct(actNames?[index]);
    fetchInviteRecord();
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }
}
